<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use think\Db;
use think\Exception;

/**
 * 技师排班控制器
 * @package app\index\controller
 */
class Scheduling extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取技师排班分类
     */
    public function getScheduling(){
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_scheduling_type')
                ->field('id,title,start_time,end_time,sort')
                ->where(['store_id'=>$userInfo['store_id']])
                ->order('sort asc')
                ->select();
            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 添加班次
     */
    public function addScheduling()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData= $this->request->post();
            $validate = new \app\api\validate\Scheduling();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['store_id'] = $userInfo['store_id'];
            $getData['created_time'] = time();
            $insert = Db::name('qt_scheduling_type')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败，请稍后再试', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 修改班次
     */
    public function saveScheduling()
    {
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $validate = new \app\api\validate\Scheduling();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['created_time'] = time();
            $insert = Db::name('qt_scheduling_type')->where(['id'=>$getData['id']])->update($getData);
            if($insert){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败，请稍后再试', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 修改班次获取班次详情
     */
    public function editScheduling()
    {
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_scheduling_type')->where(['id'=>$getData['id']])->find();
            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 换班
     */
    public function exchange(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $userInfo = $this->userInfo;
            if(!isset($getData['old_scheduling_id']) || empty($getData['old_scheduling_id'])){
                $this->result('缺少参数', '', 204);
            }
            if(!isset($getData['new_scheduling_id']) || empty($getData['new_scheduling_id'])){
                $this->result('缺少参数', '', 204);
            }
            //老班次
            $oldUserRes = Db::name('qt_users')
                ->where(['store_id'=>$userInfo['store_id'],'work_type'=>$getData['old_scheduling_id'],'role'=>'3'])
                ->select();
            //新班次
            $newUserRes = Db::name('qt_users')
                ->where(['store_id'=>$userInfo['store_id'],'work_type'=>$getData['new_scheduling_id'],'role'=>'3'])
                ->select();
            static $insertOld = [];
            $sort  = 1;
            $i = 1;
            //老的改新的
            $insertOld = [];
            foreach ($oldUserRes as $key=>$value){
                Db::name('qt_users')->where('id',$value['id'])->update(['work_type'=>$getData['new_scheduling_id']]);
                $insertOld[$i]['store_id'] = $userInfo['store_id'];
                $insertOld[$i]['engineer_id'] = $value['id'];
                $insertOld[$i]['sort'] = $sort;
                $insertOld[$i]['type_id'] = $getData['new_scheduling_id'];
                $insertOld[$i]['created_time']= time();
                $i++;
                $sort ++;
            }
            static $insertOld = [];
            $sort  = 1;
            $j = 1;
            //新的改老的
            $insertNew = [];
            if(!empty($newUserRes)){
                foreach ($newUserRes as $key=>$value){
                    Db::name('qt_users')->where('id',$value['id'])->update(['work_type'=>$getData['old_scheduling_id']]);
                    $insertNew[$j]['store_id'] = $userInfo['store_id'];
                    $insertNew[$j]['engineer_id'] = $value['id'];
                    $insertNew[$j]['sort'] = $sort;
                    $insertNew[$j]['type_id'] = $getData['old_scheduling_id'];
                    $insertNew[$j]['created_time']= time();
                    $i++;
                    $sort ++;
                }
            }
            try{
                Db::startTrans();
                Db::name('qt_scheduling_queue')
                    ->where(['type_id'=>$getData['old_scheduling_id'],'store_id'=>$userInfo['store_id']])
                    ->delete();
                Db::name('qt_scheduling_queue')->insertAll($insertOld);
                Db::name('qt_scheduling_queue')->insertAll($insertNew);
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->result('系统繁忙，请稍后再试','',204);
            }
            $this->result('换班成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 获取所有技师
     */
    public function engineerLists()
    {
        if ($this->request->isPost()) {
            $group = Db::name('qt_scheduling_type')
                ->field('id,id as work_type,title,start_time,end_time')
                ->where('store_id',$this->store_id)->select();
            $result = [];
            foreach ($group as $key => $value){
                $engineer = Db::name('qt_users')
                    ->field('id,work_number')
                    ->where(['store_id'=>$this->store_id, 'role' => '3'])
                    ->where('work_type','=',$value['id'])
                    ->select();
                foreach ($engineer as $k=>$v){
                    $engineer[$k]['work_title'] = $value['title'];
                    $engineer[$k]['key'] = $key;
                }
                $result[$key]['title'] = $value;
                $result[$key]['title']['data'] = $engineer;
            }
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 删除技师班次
     * @throws Exception
     * @throws \think\db\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function del_scheduling()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $userIds = Db::name('qt_users')->where(['store_id'=>$this->userInfo['store_id'],'work_type'=>$getData['id']])->column('id');
            try{
                //删除班次
                Db::name('qt_scheduling_type')->where('id',$getData['id'])->delete();
                //更新技师班次
                foreach ($userIds as $key=>$value){
                    Db::name('qt_users')->where('id',$value)->update(['work_type'=>0]);
                }
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
                $this->result('删除失败', '', 204);
            }
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


}
